<?php

namespace app\admin\model\meet;

use think\Model;


class Order extends Model
{


    // 表名
    protected $name = 'meet_order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'gender_text',
        'reservation_time_text',
        'pay_status_text',
        'pay_time_text',
        'order_status_text',
        'is_refund_deposit_text'
    ];

    const PAY_STATUS_UNPAY = 10;

    const PAY_STATUS_PAYED = 20;

    const ORDER_STATUS_UNPAY = 10;

    const ORDER_STATUS_CANCEL = 20;

    const ORDER_STATUS_PAYED = 30;

    const ORDER_STATUS_PROGRESS = 40;

    const ORDER_STATUS_FINISHED = 50;

    const IS_REFUND_DEPOSIT_NO = 0;

    const IS_REFUND_DEPOSIT_YES = 1;

    public function user()
    {
        return $this->belongsTo('app\\admin\\model\\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function products()
    {
        return $this->hasMany('OrderProduct', 'order_id', 'id');
    }

    public function getTotalPriceAttr($value)
    {
        return $value ? (float)$value : 0;
    }

    public function getPayPriceAttr($value)
    {
        return $value ? (float)$value : 0;
    }

    public function getOrderDepositAttr($value)
    {
        return $value ? (float)$value : 0;
    }

    public function getCouponMoneyAttr($value)
    {
        return $value ? (float)$value : 0;
    }

    public function getGenderList()
    {
        return ['0' => __('Gender 0'), '1' => __('Gender 1')];
    }

    public function getPayStatusList()
    {
        return ['10' => __('Pay_status 10'), '20' => __('Pay_status 20'), '30' => __('Pay_status 30')];
    }

    public function getOrderStatusList()
    {
        return ['10' => __('Order_status 10'), '20' => __('Order_status 20'), '30' => __('Order_status 30'), '40' => __('Order_status 40'), '50' => __('Order_status 50')];
    }

    public function getIsRefundDepositList()
    {
        return ['0' => __('Is_refund_deposit 0'), '1' => __('Is_refund_deposit 1')];
    }

    public function getIsRefundDepositTextAttr($value,$data){
        $value = $value ? $value : (isset($data['is_refund_deposit']) ? $data['is_refund_deposit'] : '');
        $list = $this->getIsRefundDepositList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getGenderTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['gender']) ? $data['gender'] : '');
        $list = $this->getGenderList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getReservationTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['reservation_time']) ? $data['reservation_time'] : '');
        return is_numeric($value) && !empty($value) ? date("Y-m-d H:i:s", $value) : '';
    }


    public function getPayStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_status']) ? $data['pay_status'] : '');
        $list = $this->getPayStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPayTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_time']) ? $data['pay_time'] : '');
        return is_numeric($value) && !empty($value) ? date("Y-m-d H:i:s", $value) : '';
    }


    public function getOrderStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['order_status']) ? $data['order_status'] : '');
        $list = $this->getOrderStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setReservationTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setPayTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
